package com.txunda.zhuohired.aty.manage.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.fgt.manage.home.ManageAllFgt;
import com.txunda.zhuohired.fgt.manage.home.ManagePratFgt;
import com.txunda.zhuohired.http.pst.CompanyPst;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/10 16:04
 * 功能描述：全部
 * 联系方式：jiahuanchen@126.com
 */

public class ManageAllAty extends BaseAty implements ManageAllFgt.FragmentInteraction {
    /**
     * LinearLayout
     */
    @ViewInject(R.id.ll_manage_all)
    private LinearLayout ll_manage_all;
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_all_job_center)
    private TextView tv_layout_all_job_center;
    /**
     * TabLayout
     */
    @ViewInject(R.id.tl_manage_all)
    private TabLayout tl_manage_all;
    /**
     * viewPager
     */
    @ViewInject(R.id.vp_manage_all)
    private ViewPager vp_manage_all;
    private ViewPagerAdapter viewPagerAdapter;
    private List<String> mTitle;
    private List<Fragment> fragments;

    private PopupWindow mPopupWindow;
    private int width;
    private CompanyPst companyPst;
    private String token;
    private int popType = 1;
    private String job_type="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_all_job_head);
        tv_layout_all_job_center.setText("全部");

    }

    @Override
    @OnClick({ R.id.tv_layout_all_job_center})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_layout_all_job_center://全部
                showPop();
                mPopupWindow.showAtLocation(ll_manage_all, Gravity.TOP, 0, 0);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_manage_all;
    }

    @Override
    protected void initialized() {
        companyPst = new CompanyPst(this);
        token = application.getUserInfo().get("data");
        mTitle = new ArrayList<>();
        mTitle.add("全职");
        mTitle.add("兼职");
        fragments = new ArrayList<>();
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        fragments.add(ManageAllFgt.newInstance(1));
        fragments.add(ManagePratFgt.newInstance(2));

    }

    private void showPop() {
        View contentView = LayoutInflater.from(ManageAllAty.this).inflate(R.layout.pop_manage_all,
                null);
        mPopupWindow = new PopupWindow(contentView);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        final TextView tv_pop_manage = contentView.findViewById(R.id.tv_pop_manage);//开放中
        final TextView tv_pop_manage_close = contentView.findViewById(R.id.tv_pop_manage_close);//已关闭
        final TextView tv_pop_manage_audit = contentView.findViewById(R.id.tv_pop_manage_audit);//审核中
        final TextView tv_pop_manage_false = contentView.findViewById(R.id.tv_pop_manage_false);//审核失败
        View view_pop_all_back = contentView.findViewById(R.id.view_pop_all_back);
        tv_pop_manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popType = 1;
                mPopupWindow.dismiss();
                tv_layout_all_job_center.setText(tv_pop_manage.getText().toString().trim());
            }
        });
        tv_pop_manage_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popType = 2;
                mPopupWindow.dismiss();
                tv_layout_all_job_center.setText(tv_pop_manage_close.getText().toString().trim());
            }
        });
        tv_pop_manage_audit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popType = 3;
                mPopupWindow.dismiss();
                tv_layout_all_job_center.setText(tv_pop_manage_audit.getText().toString().trim());
            }
        });
        tv_pop_manage_false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popType = 4;
                mPopupWindow.dismiss();
                tv_layout_all_job_center.setText(tv_pop_manage_false.getText().toString().trim());
            }
        });
        view_pop_all_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
            }
        });
    }

    @Override
    protected void requestData() {
        vp_manage_all.setAdapter(viewPagerAdapter);
        tl_manage_all.setupWithViewPager(vp_manage_all);
    }

    @Override
    public void process(String str) {
        if (str != null) {
            job_type = str;
        }
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle.get(position);
        }
    }

    public String getPopType(){
        String pType ="";
        if(popType == 1){
            pType = "1";
        }else if(popType == 2){
            pType = "2";
        }else if(popType == 3){
            pType = "3";
        }else if(popType == 4){
            pType = "4";
        }
        return pType;
    }
}
